package suites.queue.mock {
	import org.splink.library.queue.QEvent;
	import org.splink.library.queue.Qable;

	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * @author Max Kugland
	 */
	public class AsyncQable extends Qable {
		private var _timer : Timer;
		private var _progressEvent : QEvent;

		public function AsyncQable(id : String = "AsyncQable", duration : uint = 80) {
			super(id);
			_timer = new Timer(10, duration / 10);
			_progressEvent = new QEvent(this, QEvent.PROGRESS, id);
		}

		public function toString() : String {
			return "AsyncQable: " + id;
		}

		override protected function doStart() : void {
			_timer.addEventListener(TimerEvent.TIMER, run);			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
			_timer.start();
		}

		private function run(event : TimerEvent) : void {
			progress();
		}

		private function progress() : void {
			_progressEvent.progress.current = _timer.currentCount;			_progressEvent.progress.total = _timer.repeatCount;
			distribute(_progressEvent);
		}

		private function onComplete(event : TimerEvent) : void {
			_timer.removeEventListener(TimerEvent.TIMER, run);
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
			complete();
		}

		override protected function doStop() : void {
			_timer.stop();
		}

		override protected function doReset() : void {
			_timer.reset();
		}
	}
}
